const ui = {}
import {
  url_config
} from '@/common/config.js'
// 同步存储数据
ui.setStorage = (key, value) => {
  uni.setStorageSync(key, value)
}
// 同步获取数据
ui.getStorage = (key) => {
  return uni.getStorageSync(key)
}
// 移除指定key，不穿就是移除全部
ui.remove = (key) => {
  if (key) {
    uni.removeStorageSync(key)
  } else {
    uni.clearStorageSync()
  }
}
// 获取更多手机号
ui.setmorePhone = (key, value) => {
	return  uni.setStorageSync(key, value)
}
ui.getmorePhone = (key) => {
	return uni.getStorageSync(key);
}
ui.removemorePhone = (key) => {
	return uni.removeStorageSync(key)
}
// 加载
ui.loading = (title) => {
  uni.showLoading({
    title: title
  })
}
// 提示框
ui.toast = (title) => {
  uni.showToast({
    title: title,
    icon: 'none'
  });
}
// 自定义弹框
ui.show = (title, content, showCancel = false) => {
  return new Promise((resolve, reject) => {
    uni.showModal({
      title,
      content,
      showCancel,
      success: res => {
        resolve(res)
      },
      fail: (err) => {
        reject(err)
      },
    })
  })
}
ui.back = () => uni.navigateBack()
// 跳转
ui.push = (url, type = 'navigateTo') => {
  switch (type) {
    case 'navigateTo':
      uni.navigateTo({
        url
      })
      break;
    case 'redirectTo':
      uni.redirectTo({
        url
      })
      break;
    case 'reLaunch':
      uni.reLaunch({
        url
      })
      break;
    case 'switchTab':
      uni.switchTab({
        url
      })
      break;
  }
}

// 生成随机数字
ui.generateRandomAlphaNum = (len) => {
  let rdmString = "";
  for (; rdmString.length < len; rdmString += Math.random().toString(36).substr(2));
  return rdmString.substr(0, len);
}

ui.getupdate1 = (data, url) => {
  return new Promise(function(resolve, reject) {
    // let baseUrl = 'https://store.cxy365.com/';
    if (data.image.length == 0) {
      post(url, data).then(res => {
        resolve(res);
      });
      return false;
    }

    for (var i = 0; i < data.image.length; i++) {
      uni.uploadFile({
        url: url_config + url,
        filePath: data.image[i],
        headers: {
          'Accept': '*/*',
          'Content-Type': 'multipart/form-data'
        },
        success: res => {
          const fileDate = JSON.parse(res.data);
          if (fileDate.status == 200) {
            // dataFlie.data.push(fileDate.data.headimgurl)

            if (data.image.length == i) {
              resolve(fileDate);
            }
          } else {
            uni.showToast({
              title: fileDate.mess,
              duration: 2000,
              icon: "none"
            });
            reject(fileDate);
          }
        },
        fail: err => {
          console.log(JSON.stringify(err));
        }
      });
    }
  });
}
export default ui
